package com.kelvin.paopao.module.main.My;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.utils.SpUtil;
import com.kelvin.paopao.R;
import com.kelvin.paopao.injector.components.DaggerMainMyComponent;
import com.kelvin.paopao.injector.modules.MainMyModule;
import com.kelvin.paopao.module.base.BaseFragment;
import com.kelvin.paopao.module.chat.friend.FriendActivity;
import com.kelvin.paopao.module.chat.message.MessageActivity;
import com.kelvin.paopao.module.main.date.mydate.MyDateActivity;
import com.kelvin.paopao.module.market.market.market.MarketActivity;
import com.kelvin.paopao.module.market.wallet.wallet.WalletActivity;
import com.kelvin.paopao.module.peisong.list.PeisonglistActivity;
import com.kelvin.paopao.module.person.edit.PersonDetailEditActivity;
import com.kelvin.paopao.module.qrcode.webview.ScanWebViewActivity;
import com.kelvin.paopao.utils.Constant;
import com.kelvin.paopao.widgets.DrawableTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Created by Administrator on 2017/5/6.
 */

public class MainMyFragment extends BaseFragment<IMainMyPresenter> {

    @BindView(R.id.iv)
    CircleImageView iv;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    @BindView(R.id.tv_message)
    DrawableTextView tvMessage;
    @BindView(R.id.tv_unread)
    TextView tvUnread;
    @BindView(R.id.iv_bg)
    ImageView ivBg;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_main_my;
    }

    @Override
    protected void initInjector() {
        DaggerMainMyComponent.builder().
                mainMyModule(new MainMyModule(this)).build().inject(this);
    }

    @Override
    protected void initViews() {
        tvName.setText(SpUtil.getLoginUserName(getContext()));
        Glide.with(this).load(SpUtil.getLoginAvatar(getContext())).into(iv);
        if (Constant.IS_LOGIN) {
            tvLogout.setVisibility(View.VISIBLE);
        }

        setUnReadCount(getUnreadMsgCountTotal() + "");


        Glide.with(this)
                .load(SpUtil.getLoginAvatar(getContext())).centerCrop()
                .bitmapTransform(new BlurTransformation(getContext(), 25, 4))  // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                .into(ivBg);
    }


    /**
     * 获取消息数量
     *
     * @return
     */
    public int getUnreadMsgCountTotal() {
        int unreadMsgCountTotal = 0;
        int chatroomUnreadMsgCount = 0;
        unreadMsgCountTotal = EMClient.getInstance().chatManager().getUnreadMsgsCount();
        for (EMConversation conversation : EMClient.getInstance().chatManager().getAllConversations().values()) {
            if (conversation.getType() == EMConversation.EMConversationType.ChatRoom)
                chatroomUnreadMsgCount = chatroomUnreadMsgCount + conversation.getUnreadMsgCount();
        }
        return unreadMsgCountTotal - chatroomUnreadMsgCount;
    }


    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick({R.id.tv_message, R.id.tv_date, R.id.tv_friend, R.id.ll_wallet, R.id.ll_market,
            R.id.ll_peisong, R.id.tv_logout, R.id.iv, R.id.ll_about})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_message:
                MessageActivity.launch(getContext());
                break;
            case R.id.ll_about:
                ScanWebViewActivity.launch(getContext(), "http://pao.lan361.com/buss/about.aspx");
                break;
            case R.id.tv_date:
                MyDateActivity.launch(getContext());
                break;
            case R.id.tv_friend:
                FriendActivity.launch(getContext());
                break;
            case R.id.ll_wallet:
                WalletActivity.launch(getContext());
                break;
            case R.id.ll_market:
                MarketActivity.launch(getContext());
//                LocationPickActivity.launchForResult(getActivity());
                break;
            case R.id.ll_peisong:
                PeisonglistActivity.launch(getContext());
                break;
            case R.id.tv_logout:
                mPresenter.logout();
                break;
            case R.id.iv:
                PersonDetailEditActivity.
                        launch(getContext());
                break;
        }
    }

    /**
     * 设置未读个数
     */
    public void setUnReadCount(String count) {
        if (tvUnread != null) {
            tvUnread.setVisibility(View.VISIBLE);
            tvUnread.setText(count);

            if (count.equals("0")) {
                tvUnread.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 设置未读个数
     */
    public void clearUnReadCount() {
        if (tvUnread != null) {
            tvUnread.setVisibility(View.GONE);
        }
    }

}
